import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/network/xindong_response.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/routes/common/xindong_help_next_yemian.dart';
import 'package:xindong/routes/common/xindong_web_html_yemian.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/container/xindong_empty_container.dart';
import 'package:xindong/widgets/container/xindong_fenzu_container.dart';
import 'package:xindong/widgets/xindong_common_select_item.dart';

class XinDongHelpClass {
  int id = 0;
  String title = "";
  String? content;

  XinDongHelpClass();

  factory XinDongHelpClass.fromJson(Map<String, dynamic> json) {
    return XinDongHelpClass()
      ..id = json['id']
      ..title = json['title']
      ..content = json['content'];
  }
}

class XinDongHelpYeMian extends StatefulWidget {
  const XinDongHelpYeMian({super.key});

  @override
  State<XinDongHelpYeMian> createState() => _XinDongHelpYeMianState();
}

class _XinDongHelpYeMianState extends State<XinDongHelpYeMian> {
  List<List<XinDongHelpClass>>? dataList;

  @override
  void initState() {
    super.initState();

    Future.wait([
      XinDongNetworking.instance.request(XinDongApi.helpCategory),
      XinDongNetworking.instance.request(XinDongApi.helpQuestion)
    ]).then((value) {
      List list = value;
      NetworkResponse helpCategory = list.first;
      NetworkResponse helpQuestion = list.last;
      dataList = [];
      if (helpCategory.code == XinDongServerStatus.success) {
        List list = helpCategory.data;
        dataList!.add(list.map((e) => XinDongHelpClass.fromJson(e)).toList());
      } else {
        dataList!.add([]);
      }
      if (helpQuestion.code == XinDongServerStatus.success) {
        List list = helpQuestion.data;
        dataList!.add(list.map((e) => XinDongHelpClass.fromJson(e)).toList());
      } else {
        dataList!.add([]);
      }
      setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("帮助中心"),
      body: XinDongEmptyContainer(
        dataList: dataList,
        child: XinDongGroupSectionContainer(
          dataList: dataList,
          itemBuilder: (BuildContext context, int section, int index, data) {
            XinDongHelpClass model = data;
            return XinDongCommonSelectItem(
              title: "${index + 1}、${model.title}",
              value: '',
              onTap: () {
                if (section == 0) {
                  XinDongPageSingleton.instance.pushYeMian(XinDongHelpNextYeMian(model: model));
                } else {
                  XinDongPageSingleton.instance.pushYeMian(XinDongWebHtmlYeMian(title: model.title, html: model.content));
                }
              },
            );
          },
          sectionHeaderBuilder: (BuildContext context, int index) {
            return Container(
              height: 40,
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.only(left: kPaddingValue),
              child: Text(
                index == 0 ? "问题分类" : "热门问题",
                style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
              ),
            );
          },
          separatorBuilder: (BuildContext context, int index) => const SizedBox(),
        ),
      ),
    );
  }
}
